package com.taiyuan.zfmvp.ThreadPoolOperateData.service.impl;

import com.taiyuan.zfmvp.ThreadPoolOperateData.service.AsyncService;
import com.taiyuan.zfmvp.sharding.dao.TestShardingMapper;
import com.taiyuan.zfmvp.sharding.model.TestSharding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.CountDownLatch;

/**
 * @param
 * @Author: zhoufang@si-tech.com.cn
 * @Descriiption:
 * @Date 2022/11/20
 * @return
 */
@Service
public class AsyncServiceImpl implements AsyncService {


    private static final Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class.getName());


    @Resource
    private TestShardingMapper testShardingMapper;

    /**
     * 执行异步任务
     * 可以根据需求，自己加参数拟定，我这里就做个测试演示
     */
    @Override
    @Async("asyncServiceExecutor")
    public void executeAsync(CountDownLatch countDownLatch) {
        try {
            logger.info("start executeAsync");
            System.out.println("异步线程要做的事情");
            logger.info(Thread.currentThread().getName());
            TestSharding testSharding = new TestSharding();
            testSharding.setPhoneNo("111");
            testSharding.setCouponCode("111");
            testSharding.setSex(1);
            testShardingMapper.insertSelective(testSharding);
            logger.info("end executeAsync");
            countDownLatch.countDown();//计数器减1
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
